﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Kiva7.Controls;
using Kiva7.DataClasses;
using System.Collections.ObjectModel;

namespace Kiva7.Pages
{
    public partial class FriendsControl : BaseUserControl
    {
        public FriendsControl()
        {
            InitializeComponent();
        }

        private void lbxLenders_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Lender model = lbxLenders.SelectedItem as Lender;
            if (model == null)
            {
                return;
            }
            if (lbxLenders.ItemsSource is List<Lender>)
            {
                DoNavigate(LenderDetailPage.GetPageUrl(lbxLenders.ItemsSource as List<Lender>, lbxLenders.SelectedIndex));
            }
            lbxLenders.SelectedItem = null;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ListBoxItem item = GetListBoxItem(e.OriginalSource as Control);
            if (item == null)
            {
                return;
            }
            Lender l = item.DataContext as Lender;
            if (l != null)
            {
                DoNavigate(FriendEditPage.GetPageUrl(l));
            }
        }


        protected ListBoxItem GetListBoxItem(Control control)
        {
            return GetDependencyObjectFromVisualTree(control, typeof(ListBoxItem)) as ListBoxItem;
        }

        private void AddFriendClick(object sender, EventArgs e)
        {
            DoNavigate(FriendAddPage.PageUrl);
        }

    }
}
